package com.mes.web.controller.biz;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.*;
import com.mes.biz.mapper.BizCustomerMapper;
import com.mes.biz.mapper.BizCustomerPayableMapper;
import com.mes.biz.service.*;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.domain.R;
import com.mes.common.core.domain.entity.SysUser;
import com.mes.common.core.domain.model.LoginUser;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 售后单Controller
 * @author hwg
 * @email hwg
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/biz/bizAfterSaleOrder")
public class BizAfterSaleOrderController extends BaseController {

    @Autowired
    private BizAfterSaleOrderService bizAfterSaleOrderService;
    @Autowired
    private BizAfterSaleOrderAttachService bizAfterSaleOrderAttachService;
    @Autowired
    private BizOrderAttachService bizOrderAttachService;
    @Resource
    private BizCustomerPayableMapper bizCustomerPayableMapper;
    @Resource
    private BizOrderService bizOrderService;
    @Resource
    private BizCustomerPayableDetailService bizCustomerPayableDetailService;
    @Resource
    private BizCustomerPayableService bizCustomerPayableService;
    @Resource
    private  BizCustomerMapper bizCustomerService;
    @Resource
    private TokenService tokenService;

    /**
     * 查询售后单列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(BizAfterSaleOrder bizAfterSaleOrder, HttpServletRequest request, HttpServletResponse response) {
        bizAfterSaleOrder.setPage(new PageDomain(request, response));
        PageInfo<BizAfterSaleOrder> page = bizAfterSaleOrderService.findPage(bizAfterSaleOrder);
        List<BizAfterSaleOrder> list = page.getList();
        for(BizAfterSaleOrder bizAfterSaleOrder1:list) {
            //查询客户反馈图片
            List<BizAfterSaleOrderAttach> cusImgList = bizAfterSaleOrderAttachService.getAttachByOrderIdWithType(bizAfterSaleOrder1.getId(),"1");
            bizAfterSaleOrder1.setSaleOrderAttaches(cusImgList);
            //查询售后反馈图片
            List<BizAfterSaleOrderAttach> afterSaleImgList = bizAfterSaleOrderAttachService.getAttachByOrderIdWithType(bizAfterSaleOrder1.getId(),"2");
            bizAfterSaleOrder1.setAfterSaleOrderAttaches(afterSaleImgList);
            //查询安装图片
            List<BizOrderAttach> installImgList = bizOrderAttachService.getAttachByOrderIdWithType(bizAfterSaleOrder1.getOrderId(),4);
            bizAfterSaleOrder1.setInstallerAttaches(installImgList);

        }
        return R.data(page);
    }

    /**
     * 新增售后单
     */
    @Log(title = "新增售后单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated BizAfterSaleOrder bizAfterSaleOrder) {
        return R.data(bizAfterSaleOrderService.createAfterSaleOrder(bizAfterSaleOrder));
    }

    /**
     * 修改销售单
     */
    @Log(title = "修改销售单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizAfterSaleOrder bizAfterSaleOrder) {
        return R.data(bizAfterSaleOrderService.updateAfterSaleOrder(bizAfterSaleOrder));
    }

    /**
     * 小程序-售后处理
     * @param bizAfterSaleOrder
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/appletList")
    public R<PageInfo> appletList(BizAfterSaleOrder bizAfterSaleOrder, HttpServletRequest request, HttpServletResponse response) {
        bizAfterSaleOrder.setPage(new PageDomain(request, response));
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(null != loginUser){
            bizAfterSaleOrder.setProcessUserId(loginUser.getUserId());
        }
        PageInfo<BizAfterSaleOrder> page = bizAfterSaleOrderService.appletList(bizAfterSaleOrder);
        for (int i = 0; i < page.getList().size(); i++) {
            //查询客户反馈图片
            List<BizAfterSaleOrderAttach> cusImgList = bizAfterSaleOrderAttachService.getAttachByOrderIdWithType(page.getList().get(i).getId(),"1");
            page.getList().get(i).setSaleOrderAttaches(cusImgList);
            //查询售后反馈图片
            List<BizAfterSaleOrderAttach> afterSaleImgList = bizAfterSaleOrderAttachService.getAttachByOrderIdWithType(page.getList().get(i).getId(),"2");
            page.getList().get(i).setAfterSaleOrderAttaches(afterSaleImgList);
            //查询安装图片
            List<BizOrderAttach> installImgList = bizOrderAttachService.getAttachByOrderIdWithType(page.getList().get(i).getOrderId(),4);
            page.getList().get(i).setInstallerAttaches(installImgList);
        }
        return R.data(page);
    }

    @GetMapping("/info/{id}")
    public R<BizAfterSaleOrder> info(@PathVariable("id") String id) {

        BizAfterSaleOrder bizAfterSaleOrder = bizAfterSaleOrderService.get(id);
        //查询客户反馈图片
        List<BizAfterSaleOrderAttach> cusImgList = bizAfterSaleOrderAttachService.getAttachByOrderIdWithType(bizAfterSaleOrder.getId(), "1");
        bizAfterSaleOrder.setSaleOrderAttaches(cusImgList);
        //查询安装图片
        List<BizOrderAttach> installImgList = bizOrderAttachService.getAttachByOrderIdWithType(bizAfterSaleOrder.getOrderId(), 4);
        bizAfterSaleOrder.setInstallerAttaches(installImgList);

        return R.data(bizAfterSaleOrder);
    }

    /**
     * 删除售后单
     */
    @Log(title = "售后单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R remove(@PathVariable String id) {
        BizAfterSaleOrder afterSaleOrder = bizAfterSaleOrderService.get(id);
        if (!"0".equals(afterSaleOrder.getStatus())){
            return R.fail("待处理的售后单才能删除");
        }
        boolean res = bizAfterSaleOrderService.remove(afterSaleOrder);
        if(res) {
            bizAfterSaleOrderAttachService.removeBySaleOrderId(id,null);
            //删除应收
            BizOrder bizOrder =  bizOrderService.get(afterSaleOrder.getOrderId());
            BizCustomerPayable bizCustomerPayable = bizCustomerPayableService.getBizCustomerPayableByCustomerId(bizOrder.getCustomerId());
            if(bizCustomerPayable!=null) {
                BizCustomerPayableDetail bizCustomerPayableDetail = bizCustomerPayableDetailService.getByAfterSaleOrderId(id);
                if(bizCustomerPayableDetail!=null) {
                    bizCustomerPayableDetailService.remove(bizCustomerPayableDetail);
                    bizCustomerPayable.setTotalAmount(bizCustomerPayable.getTotalAmount().subtract(bizCustomerPayableDetail.getTotalAmount()));
                    bizCustomerPayable.setHasAmount(bizCustomerPayable.getHasAmount().subtract(bizCustomerPayableDetail.getTotalAmount()));
                    bizCustomerPayableMapper.update(bizCustomerPayable);
                }
            }
        }
        return R.status(true);
    }


    @Log(title = "小程序修改售后单", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAppletAfterSaleOrder")
    public R updateAppletAfterSaleOrder(@RequestBody BizAfterSaleOrder bizAfterSaleOrder) {
        return R.data(bizAfterSaleOrderService.updateAppletAfterSaleOrder(bizAfterSaleOrder));
    }

    @PostMapping("/update")
    public R update(@RequestBody BizAfterSaleOrder bizAfterSaleOrder) {
        return R.data(bizAfterSaleOrderService.update(bizAfterSaleOrder));
    }
}
